home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap09
/
b09d010.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
4KB
|
84 lines
0, Sub procedures are often used to handle
4, input in a program when information
6, comes from two or more sources and needs to
9, be put in the same format. In this
11, demonstration, a Sub procedure named AddName
14, prompts the user for input and formats
17, the text, so that it can be displayed on
19, multiple lines in a text box. The
23, procedure saves me programming time because I
25, can call it from two event procedures,
27, each associated with a different text
29, box, Sales and Marketing. Since this
34, procedure is declared in a standard module, I
36, only need to type it in one place.
39, Here's how the program works. On the Sales
41, side, if I click the Add Name button, the
45, AddName procedure is called, and I'm
48, asked to enter a Sales employee name. I can
52, type Bart. And when I click OK, it's
56, placed in the list box. Now, I can go to
58, the Marketing side and click the name.
61, And I can type a name on this side,
63, Kieran. When I click OK, it's added to
66, Marketing. On the Sales side, I can type
71, another name, Christian, and back to the Add
75, Name side. Now, in each case, the AddName
82, procedure added the name to the list
84, box, even though I clicked it on two
86, different sides. So let's quit this program
89, and take a look at the program code,
91, especially the code module that contains the
94, AddName procedure. In the first line of
99, this procedure, I create a prompt
102, string that uses the words "Enter a", the
106, Team variable, and "employee". Now the Team
111, variable comes from the TeamVariable
115, procedure call, and that's passed from the
118, calling procedure to the Sub procedure.
122, And that team will either be Sales or
124, Marketing. And in the second line, all
127, that prompt string is just placed into an
129, input box and put on the screen. And
132, those are the things we saw when we were
133, prompted to enter a name. That's what
136, customizes it for the Sales or the Marketing
139, side. This is one Sub procedure, but I
142, was able to customize it with that Team
145, variable that comes into the procedure.
147, Now, the third line builds a wrap
149, character with character 13 and character 10.
153, And that's just a combination of two
156, sort of invisible characters that will create a
159, carriage return. And what we want to do
161, is build our text box one line at a
163, time, and it should include the name that's
167, entered and then a carriage return. Now,
170, that's all returned back in a variable
172, called ReturnString, and that's sent back
175, from the AddName procedure back to the
177, calling procedure. So, let's go back now to
180, our main form and double-click one of
183, the buttons. This time we'll click AddName
186, and we'll open the Code window, and
187, we'll be able to see both the Sales and
189, Marketing event procedures. And here is
192, where the AddName procedure is called. We
196, have the name, AddName and then a call by
200, value here and a call by reference.
204, These are two procedure arguments that are
208, sent. And the return value comes back in
212, Sales position and then is added to my
215, text box in the next line. The same is
221, true in the event procedure below.
223, Marketing is sent so that the input box says
226, Marketing and we know that that's a
228, marketing name that we're going to add. Then,
230, the Marketing position variable is
232, returned because that's passed by reference.
236, And then it is added to the text box in
238, the line below. So, with two separate
241, procedure calls, I'm able to call one
244, procedure and customize it, and save myself
246, lots of typing.
248, END